<?php
declare (strict_types=1);

namespace app\api\controller\v2;

use app\common\model\ArticleCol;
use app\common\model\Article;
use app\common\model\QuestionCol;
use app\common\model\Question;
use app\common\model\TechnologyCol;
use app\common\model\Technology;

class Collection extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $user_id = $this->user_id;
        //获取所收藏的文章
        $article_id = ArticleCol::where(['col_member_id' => $user_id, 'is_col' => 1])
            ->column('article_id');
        $article = Article::where('id', 'in', $article_id)
            ->field('id,picture,title,content,zan_num,comment_num,col_num,created_at')
            ->withAttr('picture', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select()
            ->toArray();
        //获取所收藏的问答
        $question_id = QuestionCol::where(['col_member_id' => $user_id, 'is_col' => 1])
            ->column('question_id');
        $question = Question::where('id', 'in', $question_id)
            ->field('id,picture,title,content,zan_num,comment_num,col_num,created_at')
            ->withAttr('picture', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select()
            ->toArray();
        //获取所收藏的技术社区
        $technology_id = TechnologyCol::where(['col_member_id' => $user_id, 'is_col' => 1])
            ->column('technology_id');
        $technology = Technology::where('id', 'in', $technology_id)
            ->field('id,picture,title,content,zan_num,comment_num,collect_num,created_at')
            ->select()
            ->toArray();
        foreach ($technology as &$v) {
            $v['picture'] = env('app.yuming') . $v['picture'];
            $v['col_num'] = $v['collect_num'];
            unset($v['collect_num']);
        }
        $data = array_merge($article, $question, $technology);
        $count = count($data);
        return sucessMsg(200, '获取成功！', [
            'data' => $data,
            'count' => $count,
        ]);
    }
}
